package com.shemy.sshiro.config;

import com.shemy.sshiro.domain.SysUser;
import com.shemy.sshiro.service.SysUserService;
import com.shemy.sshiro.utils.JwtToken;
import com.shemy.sshiro.utils.JwtUtil;
import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description
 * @Author Dzsom
 * @Date 2019-05-14 10:51
 * @Encoding UTF-8
 * @Version 1.0
 **/
public class JWTFilter extends BasicHttpAuthenticationFilter {

    /**
     * 请求头标识
     */
    private static String LOGIN_SIGN = "Authorization";

    @Autowired
    private SysUserService userService;

    /**
     * 判断用户是否登录
     * @param request
     * @param response
     * @return
     */
    @Override
    protected boolean isLoginAttempt(ServletRequest request, ServletResponse response) {
        HttpServletRequest rq= (HttpServletRequest) request;
        String header = rq.getHeader(LOGIN_SIGN);
        return header!=null;
    }

    /**
     * 执行登录
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    protected boolean executeLogin(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest httpServletRequest= (HttpServletRequest) request;
        String header = httpServletRequest.getHeader(LOGIN_SIGN);
        JwtToken token = new JwtToken(header);
        getSubject(request,response).login(token);
//        setUser(request,response,token);
        return true;
    }

    /**
     * 是否允许访问
     */
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        if (isLoginAttempt(request,response)){
            try {
                boolean b = executeLogin(request, response);
            } catch (Exception e) {
                e.printStackTrace();

            }
        }
        return true;
    }

//    private void setUser(ServletRequest request, ServletResponse response, JwtToken token) {
//        String username =  JwtUtil.getUsername(token.getPrincipal().toString());
//        if (username!=null){
//            SysUser user = userService.findByName(username);
//            request.setAttribute("currentUser", user);
//        }
//    }

    /**
     * 跨域支持
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        httpServletResponse.setHeader("Access-control-Allow-Origin", httpServletRequest.getHeader("Origin"));
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE");
        httpServletResponse.setHeader("Access-Control-Allow-Headers", httpServletRequest.getHeader("Access-Control-Request-Headers"));
        // 跨域时会首先发送一个option请求，这里我们给option请求直接返回正常状态
        if (httpServletRequest.getMethod().equals(RequestMethod.OPTIONS.name())) {
            httpServletResponse.setStatus(HttpStatus.OK.value());
            return false;
        }
        return super.preHandle(request, response);
    }
}
